package com.jueling.culture.util;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author liyk
 * @date 2019/3/20  10:29
 */
public class DateUtil {

    public static final String FORMAT = "yyyy-MM-dd";
    public static final String FORMAT_TIME = "HH:mm";
    public static final String FORMAT_TIME_SECOND = "HH:mm:ss";
    public static final String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_CHINES = "yyyy年MM月dd日 HH:mm";
    public static final String FORMAT_DATE_CHINA = "yyyy年MM月dd日";
    public static final String FORMAT_TIMES = "yyyyMMddHHmmss";
    public static final String FORMAT_DAY = "yyyyMMdd";
    public static final String FORMAT_DATE = "yyyy-MM-dd HH:mm";
    /**
     * 用来判断分钟数是否满足一小时
     */
    private final static Integer number = 60;


    public static String datetimeToString(Date date) {
        if (date == null) {
            return null;
        }
        return dateToString(date, FORMAT_DATETIME);
    }

    public static String dateToString(Date date, String format) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date.getTime());
    }

    public static Date StringToDate(String dateStr, String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date =null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 日期加上number天
    * @return
     */
    public static Date addDay(Date date, Integer day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, day);
        return cal.getTime();
    }

    /**
     * 日期加上month月
     * @return
     */
    public static Date addMonth(Date date, Integer month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
        return cal.getTime();
    }


    /**
     * 获取到凌晨12点差秒
     *
     * @return
     */
    public static Long getTimeInTheMorning() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        // 改成这样就好了
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }


    /**
     * 时间格式化
     * @param date
     * @return
     */
    public static Date hourMinute(Date date, int day) {
        Calendar calendars = Calendar.getInstance();
        calendars.setTime(date);
        //新参数
        Date dates = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dates);
        calendar.add(Calendar.DATE, day);
        calendar.set(Calendar.HOUR_OF_DAY, calendars.get(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendars.get(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 传入日期,计算与当前日期相差天数
     *
     * @param date
     * @return
     */
    public static Integer getBetweenDay(Date date) {
        //获取当天凌晨
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        //
        int days = (int) (date.getTime() / 1000 / 60 / 60 / 24 - zero.getTime() / 1000 / 60 / 60 / 24);
        return Math.max(days,0);
    }

    public static Integer getBetweenDay(Date beginDate,Date endDate) {
        int days = (int) (endDate.getTime() / 1000 / 60 / 60 / 24 - beginDate.getTime() / 1000 / 60 / 60 / 24);
        return Math.max(days,0);
    }

    /**
     * 获取两个日期之间的天数
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static Map<String, Long> getBetweenDay(String startTime, String endTime) {
        LinkedHashMap<String, Long> result = new LinkedHashMap<>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            // 日期加1(包含结束)
            tempEnd.add(Calendar.DATE, +1);
            while (tempStart.before(tempEnd)) {
                result.put(dateFormat.format(tempStart.getTime()), 0L);
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 获取两个日期之间的月份
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static LinkedHashMap<String, LinkedHashMap<String,Long>> getBetweenMonth(String startTime, String endTime,String all,String total) {
        LinkedHashMap<String, LinkedHashMap<String,Long>> result = new LinkedHashMap<>();
        //格式化为年月
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        try {
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();

            min.setTime(sdf.parse(startTime));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

            max.setTime(sdf.parse(endTime));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

            Calendar curr = min;
            boolean flag = true;
            while (curr.before(max)|| flag) {
                LinkedHashMap<String, Long> hashMap = new LinkedHashMap<>();
                hashMap.put(all,0L);
                if (curr.before(max)) {
                    result.put(sdf.format(curr.getTime()),hashMap);
                    curr.add(Calendar.MONTH, 1);
                }else {
                    //循环最后一次 添加总计
                    result.put(total, hashMap);
                    flag = false;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 转成 XX分钟 或 XX小时XX分钟
     * @param time
     * @return
     */
    public static String minuteToHHmm(Long time) {
        String str = null;
        Long timeMin = time / 60000;
        if(timeMin >= number){
            if(timeMin%number == 0){
                str = timeMin/number + "小时";
            }else{
                str = timeMin/number + "小时" + timeMin%number + "分钟";
            }
        }else{
            str = timeMin + "分钟";
        }
        return str;
    }

    /**
     * 获取两个日期之间的天数 -- 收益分析
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static LinkedHashMap<String, LinkedHashMap<String,Long>> getBetweenDayToProfit(String startTime, String endTime,String all,String total) {
        LinkedHashMap<String, LinkedHashMap<String,Long>> result = new LinkedHashMap<>();
        //格式化为年月
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();

            min.setTime(sdf.parse(startTime));
            max.setTime(sdf.parse(endTime));
            // 日期加1(包含结束)
            max.add(Calendar.DATE, +1);
            Calendar curr = min;
            boolean flag = true;
            while (curr.before(max)|| flag) {
                LinkedHashMap<String, Long> hashMap = new LinkedHashMap<>();
                hashMap.put(all,0L);
                if (curr.before(max)) {
                    result.put(sdf.format(curr.getTime()),hashMap);
                    curr.add(Calendar.DAY_OF_MONTH, 1);
                }else {
                    //循环最后一次 添加总计
                    result.put(total, hashMap);
                    flag = false;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据今天推周一周天
     * @param time
     * @return
     */
    public static Map<String, Date> getWeekDay(Date time) {
        //格式化为年月
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Date> result = new HashMap<String, Date>();
        try {
            Calendar cal = Calendar.getInstance();

            cal.setTime(time);

            //判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
            //获得当前日期是一个星期的第几天
            int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
            if(1 == dayWeek) {
                cal.add(Calendar.DAY_OF_MONTH, -1);
            }
            cal.setFirstDayOfWeek(Calendar.MONDAY);
            //获得当前日期是一个星期的第几天
            int day = cal.get(Calendar.DAY_OF_WEEK);
            //根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
            cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);
            System.out.println("所在周星期一的日期："+sdf.format(cal.getTime()));
            result.put("firstDay",cal.getTime());
            System.out.println(cal.getFirstDayOfWeek()+"-"+day+"+6="+(cal.getFirstDayOfWeek()-day+6));
            cal.add(Calendar.DATE, 6);
            System.out.println("所在周星期日的日期："+sdf.format(cal.getTime()));
            result.put("endDay",cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
